package com.workplan.client.widgets.employeeitem;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import com.general.client.action.ActionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.workplan.shared.datamodel.employee.EmployeeDataModel;
import com.workplan.shared.datamodel.employee.EmployeeDataModel.Role;

public class EmployeeItemPresenterImpl extends
		WidgetPresenter<EmployeeItemPresenter.Display> implements
		EmployeeItemPresenter {

	private EmployeeDataModel dataModel;

	public EmployeeItemPresenterImpl(Display display, EventBus eventBus) {
		super(display, eventBus);
	}

	private void updateViewFromModel(EmployeeDataModel dataModel) {
		display.getFirstName().setText(dataModel.getFirstName());
		display.getLastName().setText(dataModel.getLastName());
		display.getPhoneNum().setText(dataModel.getPhoneNumber());
		display.getEmail().setText(dataModel.getEmail());
		display.getMinShifts().setValue(dataModel.getMinShifts());
		display.getMaxShifts().setValue(dataModel.getMaxShifts());
		display.getRole().setValue(dataModel.getRole());
	}

	@Override
	public void setModel(EmployeeDataModel dataModel) {
		this.dataModel = dataModel;
		updateViewFromModel(dataModel);
	}

	protected void persistData() {
		// TODO call service to update DM in server
	}

	@Override
	protected void onBind() {
		registerHandler(display
				.addFirstNameChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						dataModel.setFirstName(event.getValue());
						persistData();
					}
				}));

		registerHandler(display
				.addLastNameChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						dataModel.setLastName(event.getValue());
						persistData();
					}
				}));

		registerHandler(display
				.addPhoneNumChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						dataModel.setPhoneNumber(event.getValue());
						persistData();
					}
				}));

		registerHandler(display
				.addEmailChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						dataModel.setEmail(event.getValue());
						persistData();
					}
				}));

		registerHandler(display
				.addMinShiftsChangeHandler(new ValueChangeHandler<Integer>() {

					@Override
					public void onValueChange(ValueChangeEvent<Integer> event) {
						dataModel.setMinShifts(event.getValue());
						persistData();
					}
				}));

		registerHandler(display
				.addMaxShiftsChangeHandler(new ValueChangeHandler<Integer>() {

					@Override
					public void onValueChange(ValueChangeEvent<Integer> event) {
						dataModel.setMaxShifts(event.getValue());
						persistData();
					}
				}));

		registerHandler(display
				.addRoleChangeHandler(new ValueChangeHandler<EmployeeDataModel.Role>() {

					@Override
					public void onValueChange(ValueChangeEvent<Role> event) {
						dataModel.setRole(event.getValue());
						persistData();
					}
				}));

		// TODO add handler to open history modal
		// TODO add handler to open remove modal

	}

	@Override
	protected void onUnbind() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onRevealDisplay() {
	}

	@Override
	public HandlerRegistration addHistoryActionHandler(ActionHandler handler) {
		return display.addHistoryActionHandler(handler);
	}

	@Override
	public HandlerRegistration addRemoveActionHandler(ActionHandler handler) {
		return display.addRemoveActionHandler(handler);
	}

}
